package boot.spring.controller;

import boot.spring.config.ConnectionUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import boot.spring.po.Mail;
import boot.spring.po.TopicMail;
import boot.spring.service.impl.ProducerImpl;
import boot.spring.service.impl.PublisherImpl;

import java.io.IOException;
import java.util.concurrent.TimeoutException;


@Controller
public class RabbitMQController {


	@Autowired
	ProducerImpl producer;

	@Autowired
	PublisherImpl publisher;

	@RequestMapping(value="/produce",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String  produce(@ModelAttribute("mail")Mail mail) throws Exception{
		producer.sendMail("myqueue",mail);
		JSONObject ss = new JSONObject();
		ss.putOpt("status" ,"200") ;
		ss.putOpt("msg" ,"ok") ;
		return ss.toString() ;
	}

	@RequestMapping(value="/topic",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String topic(@ModelAttribute("mail")Mail mail) throws Exception{
		publisher.publishMail(mail);
		JSONObject ss = new JSONObject();
		ss.putOpt("status" ,"200") ;
		ss.putOpt("msg" ,"ok") ;
		return ss.toString() ;

	}

	@RequestMapping(value="/direct",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public void direct(@ModelAttribute("mail")TopicMail mail){
		Mail m=new Mail(mail.getMailId(),mail.getCountry(),mail.getWeight());
		publisher.senddirectMail(m, mail.getRoutingkey());
	}

	@RequestMapping(value="/mytopic",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public void topic(@ModelAttribute("mail")TopicMail mail){
		Mail m=new Mail(mail.getMailId(),mail.getCountry(),mail.getWeight());
		publisher.sendtopicMail(m, mail.getRoutingkey());
	}


	@RequestMapping("demo")
	public String demo(){
		return "demo";
	}


	@RequestMapping(value="/create_message",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public void createMessage(String msg ){
		// 获取到连接以及mq通道
		Connection connection = null;
		Channel channel = null ;
		try {
			connection = ConnectionUtil.getConnection();
			// 从连接中创建通道
			channel = connection.createChannel();
			channel.exchangeDeclare("exchange_msg_test", "direct", true, false, null);
			// 声明（创建）队列
			channel.queueDeclare("create.msg.test", true, false, false, null);
			// 消息内容
			channel.basicPublish("exchange_msg_test", "create.msg.test", null, msg.getBytes());
			System.out.println(" [x] Sent '" + msg + "'");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//关闭通道和连接
			try {
				channel.close();
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
		}
	}



}
